package com.huizhonglm.diap.presenters.tab;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.app.banner.BannerInfo;
import com.huizhonglm.diap.api.model.app.banner.BannerListParams;
import com.huizhonglm.diap.api.model.shop.FeaturedBuyInfo;
import com.huizhonglm.diap.api.model.shop.FeaturedBuyParams;
import com.huizhonglm.diap.api.model.shop.ShopInfo;
import com.huizhonglm.diap.api.model.shop.ShopListParams;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.location.LocationUtil;
import com.huizhonglm.diap.core.storage.model.LocationInfo;
import com.huizhonglm.diap.model.Shortcut;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.tab.iView.TabhostHomeIView;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-07 13:53
 */
public class TabhostHomePresenter extends BasePresenter {

    private TabhostHomeIView iView;

    private int pageIndex;

    public TabhostHomePresenter(TabhostHomeIView iView) {
        this.iView = iView;
    }

    public void loadBanner(){
        API.http().post("bannerList", new BannerListParams(), new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<BannerInfo> list = JSON.parseArray(result, BannerInfo.class);
                    iView.onLoadBannerSuccess(list);
                } else {
                    iView.onLoadBannerFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadBannerFailure();
            }
        });
    }

    public List<Shortcut> loadShortcuts(){
//        1://美食
//        2://电影
//        3://外卖
//        4://酒店
//        5://KTV
//        6://到店付
//        7://代金券
//        8://旅游
        Shortcut food = new Shortcut();
        food.setDrawable(R.drawable.ic_shortcut_food);
        food.setId("1");
        food.setName("美食");
        Shortcut movie = new Shortcut();
        movie.setDrawable(R.drawable.ic_shortcut_movie);
        movie.setId("2");
        movie.setName("电影");
        Shortcut takeout = new Shortcut();
        takeout.setDrawable(R.drawable.ic_shortcut_takeout);
        takeout.setId("3");
        takeout.setName("外卖");
        Shortcut hotels = new Shortcut();
        hotels.setDrawable(R.drawable.ic_shortcut_hotels);
        hotels.setId("4");
        hotels.setName("酒店");
        Shortcut ktv = new Shortcut();
        ktv.setDrawable(R.drawable.ic_shortcut_ktv);
        ktv.setId("5");
        ktv.setName("KTV");
        Shortcut shoppay = new Shortcut();
        shoppay.setDrawable(R.drawable.ic_shortcut_shoppay);
        shoppay.setId("6");
        shoppay.setName("到店付");
        Shortcut coupon = new Shortcut();
        coupon.setDrawable(R.drawable.ic_shortcut_coupon);
        coupon.setId("7");
        coupon.setName("代金券");
        Shortcut tourism = new Shortcut();
        tourism.setDrawable(R.drawable.ic_shortcut_tourism);
        tourism.setId("8");
        tourism.setName("旅游");
        List<Shortcut> data = new ArrayList<>();
        data.add(food);
        data.add(movie);
        data.add(takeout);
        data.add(hotels);
        data.add(ktv);
        data.add(shoppay);
        data.add(coupon);
        data.add(tourism);
        return data;
    }

    public void loadFeaturedBuy(){
        FeaturedBuyParams params = new FeaturedBuyParams();
        LocationInfo loc = LocationUtil.getLocation();
        if (loc != null) {
            params.lat = loc.getLatitude();
            params.lng = loc.getLongitude();
        } else {
            params.lat = Constants.LATITUDE;
            params.lng = Constants.LONGITUDE;
        }
        params.radius = Constants.RADIUS;
        API.http().post("featuredBuy", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<FeaturedBuyInfo> list = JSON.parseArray(result, FeaturedBuyInfo.class);
                    iView.onLoadFeaturedBuySuccess(list);
                } else {
                    iView.onLoadFeaturedBuyFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadFeaturedBuyFailure();
            }
        });
    }

    public void loadShopList(final String pullFlag){
        switch (pullFlag) {
            case Constants.LOAD_INIT:
            case Constants.LOAD_DROP:
                pageIndex = 1;
                break;
            case Constants.LOAD_MORE:
                pageIndex ++;
                break;
        }
        ShopListParams params = new ShopListParams(API.buildUri(API.HOME_SHOP_LIST));
        LocationInfo loc = LocationUtil.getLocation();
        if (loc != null) {
            params.lat = loc.getLatitude();
            params.lng = loc.getLongitude();
        } else {
            params.lat = Constants.LATITUDE;
            params.lng = Constants.LONGITUDE;
        }

        params.radius = Constants.RADIUS;
        params.pageNumber = pageIndex;
        API.http().post("homeShopList", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<ShopInfo> list = JSON.parseArray(result, ShopInfo.class);
                    iView.onLoadShopListSuccess(pullFlag, list);
                } else {
                    iView.onLoadShopListFailure(pullFlag);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadShopListFailure(pullFlag);
            }
        });
    }
}
